package com.remindurself.action.base;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


/** 
 * @author  Bruce 
 * @date 2011-8-4
 * @comment
 */
public class BaseAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1281824092820429037L;

	private HttpServletRequest request = null;
	private HttpServletResponse response = null;
	private Map<String, Object> session = null;
	private ServletContext servletContext;
	
	@Override
	public String execute() throws Exception {
		/*ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);*/
		/*User user = (User) session.get("user");
		if (user == null){
			return "login";
		}*/
		return super.execute();
	}
	
	@Override
	public String input() throws Exception {
		
		return super.input();
	}

	
	@Override
	public void validate() {
		
		super.validate();
	}

	public HttpServletRequest getRequest() throws UnsupportedEncodingException{
		request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		return request;
	}
	
	public HttpServletResponse getResponse(){
		response = ServletActionContext.getResponse();
		//response.setContentType("text/json; charset=utf-8");
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		return response; 
	}
	/**
	 * 获得工程绝对路径
	 * @return
	 */
	public String getBaseRealPath(){
		servletContext = ServletActionContext.getServletContext();
		return servletContext.getRealPath("/"); 
	}
	public Map<String, Object> getSession(){
		return ActionContext.getContext().getSession();
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
